package com.micro.center.controller;

import com.micro.center.dto.SysUserDto;
import com.micro.center.service.SysUserService;
import com.micro.common.aop.PermissionRole;
import com.micro.common.constant.Constant;
import com.micro.common.constant.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author pan.shun
 * @since 2022/7/5 11:15
 */
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 登录
     */
    @PostMapping("/login")
    public R login(@RequestBody SysUserDto sysUserDto) {
        return sysUserService.login(sysUserDto);
    }

    /**
     * 注册
     */
    @PostMapping("/register")
    public R register(@RequestBody SysUserDto sysUserDto) {
        return sysUserService.registerUser(sysUserDto);
    }

    /**
     * 退出登录
     */
    @GetMapping("/loginOut")
    public R loginOut(HttpServletRequest request) {
        return sysUserService.loginOut(request);
    }

    /**
     * 查看在线用户
     */
    @PermissionRole(role = Constant.ROLE.ADMIN)
    @GetMapping("/onlineUser")
    public R getOnlineUser(HttpServletRequest request) {
        return sysUserService.getOnlineUser(request);
    }

    /**
     * 强制下线
     */
    @PermissionRole(role = Constant.ROLE.ADMIN)
    @GetMapping("/forcedOffline/{tokenId}")
    public R forcedOffline(@PathVariable String tokenId) {
        return sysUserService.forcedOffline(tokenId);
    }


}
